package com.example.liteflow.service;

import com.example.liteflow.constant.DynamicConstant;
import com.example.liteflow.entity.Interest;
import com.example.liteflow.entity.User;
import com.example.liteflow.mapper.UserMapper;
import com.yomahub.liteflow.builder.LiteFlowNodeBuilder;
import com.yomahub.liteflow.builder.el.ELBus;
import com.yomahub.liteflow.builder.el.ELWrapper;
import com.yomahub.liteflow.builder.el.LiteFlowChainELBuilder;
import com.yomahub.liteflow.core.FlowExecutor;
import com.yomahub.liteflow.flow.LiteflowResponse;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * 2025/9/5.
 *
 * @author haijin
 */
@Service
@Slf4j
public class FlowService {

    @Resource
    private FlowExecutor flowExecutor;

    @Resource
    private UserService userService;

    @Resource
    private InterestService interestService;


//    @Transactional(rollbackFor = Exception.class)
    public String exec() throws Exception {
        ELWrapper el = ELBus.then("a");


//        User user = new User();
//        user.setName("test");
//        user.setAddress("深圳");
//
//        userService.save(user);
        System.out.println(el.toEL(true));


        LiteFlowChainELBuilder.createChain().setChainId("t123").setEL(
                // 输出el表达式
                el.toEL()
        ).build();

        //构建一个普通脚本组件
        LiteFlowNodeBuilder.createScriptNode().setId("a")
                .setName("组件A")
                .setScript(DynamicConstant.TEST_3_1)
                .build();


        LiteflowResponse liteflowResponse = flowExecutor.execute2Resp("t123", null, null);
//
//        if (!liteflowResponse.isSuccess()) {
//            throw  liteflowResponse.getCause();
//        }

//        int i = 1/0;
//
//        Interest interest = new Interest();
//        interest.setName("游泳");
//        interest.setCreateTime(LocalDateTime.now());
//
//        interestService.saveInterest(interest);

        return "ok";
    }



}
